#!/usr/bin/python

# Imports
import sys
import pygame
import scene

class Inputbox(object):
    """Used when you want to know certain information which should be entered through key events."""

    def __init__(self, screen, sql, scene):
        """Initialization of Inputbox.
        Creates the necessary global parameters.
        Parameters: display screen - drawing screen
                    SQL sql - instance of SQL()
                    Scene scene - instance of Scene()"""
        print 'Initializing Inputbox'
        self.screen = screen
        self.question = ''
        self.sql = sql
        self.scene = scene
        self.keepGoing = True
        self.existing_user = True
        self.current_string = []
        self.img = None
        self.font = pygame.font.Font(None, 25)
    
    def set_question(self, question):
        """Sets the question shown in the inputbox.
        Parameters: string question - what you want to know"""
        self.question = question
    
    def display_box(self, screen, question, division):
        """Draws the inputbox.
        Parameters: display screen - drawing screen
                    string question - message shown in the inputbox (which is what you want to know)
                    double division - y-coord is devided by this number"""
        x = screen.get_width() / 2
        y = screen.get_height() / division
        rect = pygame.Rect((0, 0, 500, 30))
        pygame.draw.rect(screen, (255,255,255), ((x - rect.width / 2),  (y - rect.height / 2), rect.width, rect.height), 0)
        if len(question) != 0:
            screen.blit(self.font.render(question, 1, (0,0,0)), (x - rect.width / 2, y - rect.height / 3.5))

    def ask(self, events, division = 2):
        """Checks all the key events while the inputbox is being drawn.
        If a letter is pressed this letter is added to a string list.
        If backspace is pressed the last letter is removed from the string list.
        Returns the string list as a string.
        Parameters: event events - pygame.event.get()
                    double division - y-coord is devided by this number"""
        if not self.existing_user:
            self.scene.draw_text("No face was detected or the entered ID code does not exist.", 28, (255, 0, 0), 'CENTER', 335)
        self.display_box(self.screen, self.question + ": " + pygame.string.join(self.current_string,""), division)
    
        for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_BACKSPACE:
                    self.current_string = self.current_string[0:-1]
                elif event.key == pygame.K_MINUS:
                    self.current_string.append("_")
                elif event.key <= 127:
                    self.current_string.append(chr(event.key))
                    
        return pygame.string.join(self.current_string,"")
    
    def reset(self):
        """Resets the inputbox cleaning the string list."""    
        self.current_string = []
        self.existing_user = True
